package org.xshop.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AbstractController;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.UploadUtil;
import top.ibase4j.core.util.WebUtil;

@RestController
@RequestMapping("/app/")
@Api(tags = "File", description = "上传文件")
public class FileController extends AbstractController {

	@PostMapping("/file/upload.api")
	@ApiOperation(value = "上传图片", notes = "上传图片", produces = MediaType.APPLICATION_JSON_VALUE)
	public Object upload(HttpServletRequest request) {
		Map<String, Object> map = WebUtil.getParameter(request);
		List<String> imgs = UploadUtil.uploadImage(request, false);
		List<String> fileImgs = InstanceUtil.newArrayList();
		if (imgs != null) {
			for (String string : imgs) {
				String filePath = UploadUtil.getUploadDir(request) + string;
				fileImgs.add(UploadUtil.remove2FDFS(filePath).getRemotePath());
			}
		}
		ModelMap modelMap = new ModelMap("data",map);
		return setSuccessModelMap(modelMap, fileImgs);
	}
}
